package com.example.invoicing.controller;

import com.example.invoicing.pojo.Inbound;
import com.example.invoicing.pojo.InboundOutbound;
import com.example.invoicing.pojo.PageResult;
import com.example.invoicing.service.InboundOutboundService;
import com.example.invoicing.util.DateUtil;
import com.example.invoicing.util.ReturnUtil;
import com.example.invoicing.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class InboundOutboundJsonController {
    @Autowired
    private InboundOutboundService inboundOutboundService;

    //删除一条数据
    @ResponseBody
    @RequestMapping(value = "/member/deleteInboundOutbound", method = RequestMethod.POST)
    public ModelMap deleteInboundOutbound(@Validated @RequestBody InboundOutbound inboundOutbound, BindingResult result) {
        try {
            if(StringUtils.isEmpty(inboundOutbound.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            int count = inboundOutboundService.deleteByPrimaryKey(inboundOutbound.getId());
            if (count == 0){
                return ReturnUtil.error("删除失败");
            }

            return ReturnUtil.success("删除数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //添加一条数据
    @ResponseBody
    @RequestMapping(value = "/member/addInboundOutbound", method = RequestMethod.POST)
    public ModelMap addInboundOutbound(@Validated @RequestBody InboundOutbound inboundOutbound, BindingResult result) {
        try {

            //随机产生一组加密字符
            inboundOutbound.setId(UuidUtil.getUUID());
            //获取到当前时间
            inboundOutbound.setCreateTime(DateUtil.getCurrentTime());
            int count = inboundOutboundService.insertSelective(inboundOutbound);
            if (count == 0){
                return ReturnUtil.success("添加数据失败");
            }

            return ReturnUtil.success("添加数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //查询一条数据
    @ResponseBody
    @RequestMapping(value = "/member/queryInboundOutbound", method = RequestMethod.POST)
    public ModelMap queryInboundOutbound(@Validated @RequestBody InboundOutbound inboundOutbound, BindingResult result) {
        try {

            if(StringUtils.isEmpty(inboundOutbound.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            InboundOutbound inboundOutbound1 = inboundOutboundService.selectByPrimaryKey(inboundOutbound.getId());
            if (inboundOutbound1 == null){
                return ReturnUtil.error("查询数据失败");
            }

            return ReturnUtil.success("查询数据成功", inboundOutbound1);

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //更新一条数据
    @ResponseBody
    @RequestMapping(value = "/member/updateInboundOutbound", method = RequestMethod.POST)
    public ModelMap updateInboundOutbound(@Validated @RequestBody InboundOutbound inboundOutbound, BindingResult result) {
        try {

            if(StringUtils.isEmpty(inboundOutbound.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            //更新修改时间
            inboundOutbound.setUpdateTime(DateUtil.getCurrentTime());

            int count = inboundOutboundService.updateByPrimaryKeySelective(inboundOutbound);
            if (count == 0){
                return ReturnUtil.error("更新数据失败");
            }

            return ReturnUtil.success("更新数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }


    //集合于分页查询，所有查询，模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allInboundOutboundList",method = RequestMethod.POST)
    public ModelMap allInboundOutboundList(@Validated @RequestBody InboundOutbound inboundOutbound, BindingResult result){
        try {

            if (!StringUtils.isEmpty(inboundOutbound.getUserType())){
                if (inboundOutbound.getUserType().equals("1")){

                }else if (inboundOutbound.getUserType().equals("2")){

                }
            } else {

            }

            List<InboundOutbound> inboundOutboundList = inboundOutboundService.queryInboundOutboundList(inboundOutbound);

            if (inboundOutboundList.size() == 0){
                return ReturnUtil.error("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = inboundOutboundService.queryCountLike(inboundOutbound);

            //加载实体将数据返回
            PageResult pageResult = new PageResult();
            if (inboundOutbound.getPageResult() != null){
                pageResult.setCurrentPage(inboundOutbound.getPageResult().getCurrentPage());
                pageResult.setPageSize(inboundOutbound.getPageResult().getPageSize());
            }
            pageResult.setTotalSize(countLike);
            pageResult.setItems(inboundOutboundList);

            return ReturnUtil.success("查询数据成功", pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

}